<style>
    /* 根据字数修改 */
    .layui-form-label {
        width: 60px;
    }
</style>
<div class="layuimini-container layuimini-page-anim">
    <!-- 用于创建子页面 -->
    <div class="page-create"></div>
    <!-- page-main-create -->
    <!-- 用于显示 隐藏 -->
    <div class="layuimini-main page-main-create">
        <div class="title-row pb-0">
            <div class="title-left">
                <div class="title-info">
                    <div class="layui-tabs layui-hide-v noborder" id="planTabs">
                        <ul class="layui-tabs-header">
                            <li lay-closable="false">周期性计划</li>
                            <li lay-closable="false">临时计划</li>
                        </ul>
                    </div>
                </div>
            </div>
            <div class="title-right">
                <span class="title-right-text">
                    待完成计划
                    <b>0</b>
                    即将到期计划
                    <b>0</b>
                    <i class="layui-icon layui-icon-tips" lay-on="question_tips"></i>
                    <button type="button" class="layui-btn  layui-btn-sm layui-btn-primary ml-3"
                        lay-on="maps_look">地图全览</button>
                </span>
            </div>
        </div>
        <hr class="m-0" />
        <div class="m-df">
            <form class="layui-form" action="" lay-filter="search-form">
                <div class="layui-form-item">
                    <div class="search-box-wrapper">
                        <div class="search-box-body">
                            <div class="layui-inline">
                                <label class="layui-form-label">计划名称</label>
                                <div class="layui-input-inline">
                                    <input type="text" placeholder="请输入" name="name" autocomplete="off"
                                        class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">业务类型</label>
                                <div class="layui-input-inline">
                                    <select class="layui-input" lay-append-to="body" placeholder="请选择"
                                        name="businessNames" lay-search>
                                        <option value="1">清扫保洁</option>
                                        <option value="2">绿化养护</option>
                                        <option value="3">设施维护</option>
                                        <option value="4">其他</option>
                                    </select>


                                </div>
                            </div>

                            <div class="layui-inline">
                                <label class="layui-form-label">执行人</label>
                                <div class="layui-input-inline">
                                    <input type="text" placeholder="请输入" name="city" autocomplete="off"
                                        class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">起始日期</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="datePeriodStart" autocomplete="off" id="date_period_start"
                                        class="layui-input" placeholder="请选择">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">截止日期</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="datePeriodEnd" autocomplete="off" id="date_period_end"
                                        class="layui-input" placeholder="请选择">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">循环方式</label>
                                <div class="layui-input-inline">
                                    <input type="text" placeholder="请输入" name="cronFreqs" autocomplete="off"
                                        class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">作业路段</label>
                                <div class="layui-input-inline">
                                    <input type="text" placeholder="请输入" name="lineNameList" autocomplete="off"
                                        class="layui-input">
                                </div>
                            </div>

                        </div>
                        <div class="search-box-btns">
                            <div class="switch-wrapper" id="toggleFormBtn">
                                <span>展开</span>
                                <i class="layui-icon layui-icon-down" id="toggleIcon"></i>
                            </div>
                            <button type="submit" class="layui-btn layui-btn-sm" lay-submit
                                lay-filter="data-search-btn">查询</button>
                            <button type="reset" class="layui-btn  layui-btn-sm layui-btn-primary">重置</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
        <div class="section">
            <!-- 周期性计划 -->
            <div class="layui-tabs layui-tabs-card noborder" id="periodicPlan">
                <ul class="layui-tabs-header">
                    <li lay-id="periodicPlan1">全部 (36)</li>
                    <li lay-id="periodicPlan2">未开始 (0)</li>
                    <li lay-id="periodicPlan3">进行中 (0)</li>
                    <li lay-id="periodicPlan4">已结束 (36)</li>
                </ul>
            </div>
            <!-- 临时计划 -->
            <div class="layui-tabs layui-tabs-card noborder" id="temporaryPlan">
                <ul class="layui-tabs-header">
                    <li lay-id="temporaryPlan1">全部 (0)</li>
                    <li lay-id="temporaryPlan2">进行中 (0)</li>
                    <li lay-id="temporaryPlan3">已结束 (0)</li>
                </ul>
            </div>


            <div class="table-stripe">
                <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
            </div>
        </div>
        <script type="text/html" id="currentTableBar">
            <a class="layui-font-blue mr-3" lay-event="detail">详情</a>
            <!-- <a class="layui-font-blue mr-3" lay-event="edit">编辑</a>
            <a class="layui-font-blue" lay-event="delete">删除</a> -->
        </script>
    </div>
</div>
<script>
    layui.use(['form', 'table', 'tabs', 'miniPage', 'element'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            tabs = layui.tabs,
            laydate = layui.laydate,
            layer = layui.layer,
            util = layui.util,
            element = layui.element,
            miniPage = layui.miniPage;
        /******************** 事件 start ********************/

        util.on('lay-on', {
            // 问号事件
            'question_tips': function () {
                layer.tips(`<p>待完成计划：包括进行中、未开始的计划</p>
                            <p>即将到期计划：距作业截止日期3天及以内的计划</p>`, this, {
                });
            },
            //地图单页面赋值
            'maps_look': function () {
                miniPage.renderPageCreate('page/laborOperations/laborJobManager/components/map.html')
            }
        })
        /******************** 事件 end ********************/



        /******************** 渲染组件 start ********************/
        form.render('select'); //刷新select选择框渲染
        // 起始日期
        laydate.render({
            elem: '#date_period_start',
            range: true
        });
        // 截止日期
        laydate.render({
            elem: '#date_period_end',
            range: true
        });
        // 初始化选项卡
        tabs.render({
            elem: '#planTabs',
            index: 0// 默认选中
        });
        // tabs 切换后的事件
        function handleTabSwitch(tabIndex) {
            if (tabIndex === 0) {
                $('#periodicPlan').show();
                $('#temporaryPlan').hide();
            } else if (tabIndex === 1) {
                $('#periodicPlan').hide();
                $('#temporaryPlan').show();
            }
        }

        // planTabs 切换事件
        tabs.on('afterChange(planTabs)', function (data) {
            var tabIndex = data.index;
            console.log('tabIndex==', tabIndex)

            handleTabSwitch(tabIndex); // 调用处理函数
        });

        // 页面加载完成后默认执行一次
        $(document).ready(function () {
            var defaultIndex = 0; // 默认选中第一个 tab
            handleTabSwitch(defaultIndex);
        });


        // 周期
        tabs.render({
            elem: '#periodicPlan',
            index: 3 // 默认选中
        });
        // tabs 切换后的事件
        tabs.on('afterChange(periodicPlan)', function (data) {
            console.log('afterChange===', data);
            console.log(data.index); // 切换前的选中标签索引
        });

        // 临时
        tabs.render({
            elem: '#temporaryPlan',
            index: 2 // 默认选中
        });
        // tabs 切换后的事件
        tabs.on('afterChange(temporaryPlan)', function (data) {
            console.log('afterChange===', data);
            console.log(data.index); // 切换前的选中标签索引
        });

        /******************** 渲染组件 end ********************/

        table.render({
            elem: '#currentTableId',
            url: 'api/table.json',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter'],
            cols: [[
                { type: "checkbox", width: 50 },
                { field: 'id', width: 80, title: 'ID', sort: true },
                { field: 'name', minWidth: 180, title: '作业计划名称' },
                { field: 'businessNames', width: 100, title: '业务类型' },
                { field: 'city', width: 80, title: '状态' },
                { field: 'time', title: '起始时间', minWidth: 150 },
                { field: 'time', title: '截止时间', minWidth: 150, sort: true },
                { field: 'score', width: 80, title: '作业路段' },
                { field: 'classify', width: 80, title: '执行人' },
                { field: 'wealth', width: 135, title: '循环方式' },
                { field: 'time', width: 135, title: '创作时间' },
                { title: '操作', minWidth: 100, toolbar: '#currentTableBar', align: "center" }
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'grid',
            size: 'lg'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            layer.alert(result, {
                title: '最终的搜索信息'
            });

            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: result
                }
            }, 'data');

            return false;
        });

        /**
         * toolbar事件监听
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {   // 监听添加操作
                var content = miniPage.getHrefContent('page/table/add.html');
                var openWH = miniPage.getOpenWidthHeight();

                var index = layer.open({
                    title: '添加用户',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            console.log('tool(currentTableFilter)====', obj)
            if (obj.event === 'edit') {
                var content = miniPage.getHrefContent('page/table/add.html');
                var openWH = miniPage.getOpenWidthHeight();

                var index = layer.open({
                    title: '编辑用户',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确认删除吗', function (index) {
                    obj.del();
                    layer.close(index);
                });
            } else if (obj.event === 'detail') {  // 监听详情操作
                miniPage.renderPageCreate(`page/laborOperations/laborJobManager/components/planDetail.html?id=${data.id}`)
            }
        });

        // 开启监听 折叠展开事件
        miniPage.listen()

    });
</script>